<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\DistributionTeam
 *
 * @property int $id
 * @property int $user_id 用户id
 * @property int $next_user_id 分享到的用户id
 * @property int $special_appointment_user_id 特约分销商
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionTeam newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionTeam newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\DistributionTeam onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionTeam query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionTeam whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionTeam whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionTeam whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionTeam whereNextUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionTeam whereRuleId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionTeam whereSpecialAppointmentUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionTeam whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionTeam whereUserId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\DistributionTeam withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\DistributionTeam withoutTrashed()
 * @mixin \Eloquent
 * @property int $rule_id 当前的分销规则
 */
class DistributionTeam extends Model
{
    //
    use SoftDeletes;
    public static function joinMyTeam($new_user_id,$invited_user_id = 0){

        if ($invited_user_id == 0)
            return 0;
        if ($new_user_id == $invited_user_id)
            return 0;
        $exist = DistributionTeam::whereNextUserId($new_user_id)->first();
        if ($exist){
            return 0;
        }

        $join = new DistributionTeam();
        $join->user_id      = $invited_user_id;
        $join->next_user_id = $new_user_id;
        $join->save();
        return 1;
    }
}
